package net.oschina.android.model;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import net.oschina.android.app.AppException;
import net.oschina.android.util.StringUtil;
import net.oschina.android.util.VolleyRequestQueue;
import net.oschina.android.util.XmlRequest;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;

/**
 * 动弹列表实体类
 * @author liux (http://my.oschina.net/liux)
 * @version 1.0
 * @created 2012-3-21
 */
public class TweetList extends Entity{
	static TweetList tweetlistInstance = new TweetList();
	static Tweet tweet = null;
	
	public final static int CATALOG_LASTEST = 0;
	public final static int CATALOG_HOT = -1;

	private int pageSize;
	private int tweetCount;
	private List<Tweet> tweetlist = new ArrayList<Tweet>();
	
	public int getPageSize() {
		return pageSize;
	}
	public int getTweetCount() {
		return tweetCount;
	}
	public List<Tweet> getTweetlist() {
		return tweetlist;
	}

	public static TweetList parse(Context context,String url) throws IOException, AppException {
		
		XmlRequest xmlRequest = new XmlRequest(context,url, new Response.Listener<XmlPullParser>() {
			@Override
			public void onResponse(XmlPullParser response) {
				
		        try {        	
		            //获得解析到的事件类别，这里有开始文档，结束文档，开始标签，结束标签，文本等等事件。
		            int evtType=response.getEventType();
					//一直循环，直到文档结束    
					while(evtType!=XmlPullParser.END_DOCUMENT){ 
			    		String tag = response.getName(); 
					    switch(evtType){ 
					    	case XmlPullParser.START_TAG:
					    		if(tag.equalsIgnoreCase("tweetCount")) 
					    		{
					    			tweetlistInstance.tweetCount = StringUtil.toInt(response.nextText(),0);
					    		}
					    		else if(tag.equalsIgnoreCase("pageSize")) 
					    		{
					    			tweetlistInstance.pageSize = StringUtil.toInt(response.nextText(),0);
					    		}
					    		else if (tag.equalsIgnoreCase(Tweet.NODE_START)) 
					    		{ 
					    			tweet = new Tweet();
					    		}
					    		else if(tweet != null)
					    		{	
						            if(tag.equalsIgnoreCase(Tweet.NODE_ID))
						            {			      
						            	tweet.id = StringUtil.toInt(response.nextText(),0);
						            }
						            else if(tag.equalsIgnoreCase(Tweet.NODE_FACE))
						            {			            	
						            	tweet.setFace(response.nextText());
						            }
						            else if(tag.equalsIgnoreCase(Tweet.NODE_BODY))
						            {			            	
						            	tweet.setBody(response.nextText());		            	
						            }
						            else if(tag.equalsIgnoreCase(Tweet.NODE_AUTHOR))
						            {			            	
						            	tweet.setAuthor(response.nextText());		            	
						            }
						            else if(tag.equalsIgnoreCase(Tweet.NODE_AUTHORID))
						            {			            	
						            	tweet.setAuthorId(StringUtil.toInt(response.nextText(),0));		            	
						            }
						            else if(tag.equalsIgnoreCase(Tweet.NODE_COMMENTCOUNT))
						            {			            	
						            	tweet.setCommentCount(StringUtil.toInt(response.nextText(),0));			            	
						            }
						            else if(tag.equalsIgnoreCase(Tweet.NODE_PUBDATE))
						            {			            	
						            	tweet.setPubDate(response.nextText());	
						            }
						            else if(tag.equalsIgnoreCase(Tweet.NODE_IMGSMALL))
						            {			            	
						            	tweet.setImgSmall(response.nextText());			            	
						            }
						            else if(tag.equalsIgnoreCase(Tweet.NODE_IMGBIG))
						            {			            	
						            	tweet.setImgBig(response.nextText());			            	
						            }
						            else if(tag.equalsIgnoreCase(Tweet.NODE_APPCLIENT))
						            {			            	
						            	tweet.setAppClient(StringUtil.toInt(response.nextText(),0));				            	
						            }
					    		}
					            //通知信息
					            else if(tag.equalsIgnoreCase("notice"))
					    		{
					            	tweetlistInstance.setNotice(new Notice());
					    		}
					            else if(tweetlistInstance.getNotice() != null)
					    		{
					    			if(tag.equalsIgnoreCase("atmeCount"))
						            {			      
					    				tweetlistInstance.getNotice().setAtmeCount(StringUtil.toInt(response.nextText(),0));
						            }
						            else if(tag.equalsIgnoreCase("msgCount"))
						            {			            	
						            	tweetlistInstance.getNotice().setMsgCount(StringUtil.toInt(response.nextText(),0));
						            }
						            else if(tag.equalsIgnoreCase("reviewCount"))
						            {			            	
						            	tweetlistInstance.getNotice().setReviewCount(StringUtil.toInt(response.nextText(),0));
						            }
						            else if(tag.equalsIgnoreCase("newFansCount"))
						            {			            	
						            	tweetlistInstance.getNotice().setNewFansCount(StringUtil.toInt(response.nextText(),0));
						            }
					    		}
					    		break;
					    	case XmlPullParser.END_TAG:	
							   	//如果遇到标签结束，则把对象添加进集合中
						       	if (tag.equalsIgnoreCase("tweet") && tweet != null) { 
						       		tweetlistInstance.getTweetlist().add(tweet); 
						       		tweet = null; 
						       	}
						       	break; 
					    }
					    //如果xml没有结束，则导航到下一个节点
					    evtType=response.next();
					}		
		        } catch (XmlPullParserException e) {
					try {
						throw AppException.xml(e);
					} catch (AppException e1) {
						e1.printStackTrace();
					}
		        } catch (IOException e) {
					e.printStackTrace();
				} 
				
				
				
			}
		}, new Response.ErrorListener() {

			@Override
			public void onErrorResponse(VolleyError response) {
			}
		});
		
		VolleyRequestQueue.getInstance(context);
		RequestQueue queue = VolleyRequestQueue.getRequestQueue();
		queue.add(xmlRequest);

        return tweetlistInstance;       
	}
}
